<?php
/**
 * Created by PhpStorm.
 * User: andrei
 * Date: Jul 12, 2012
 * Time: 1:57:41 PM
 * To change this template use File | Settings | File Templates.
 */

class Db_Products_MemoryList extends Common {
	protected static $tableName = TAB_MEMORY_LIST;
	protected static $dbFields = array(
        'id',
		'ml_memory_identifier',
		'ml_gallery_id',
		'ml_date',
		'ml_project_id',
        'deleted'
	);


	/**
	 * @var Db_Products_MemoryList
	 */
	public $objectArray;

	public $id;
	public $ml_memory_identifier;
	public $ml_gallery_id;
	public $ml_date;
	public $ml_project_id;
    public $deleted;

    function __construct($value=NULL, $fieldName=NULL) {
        parent::__construct($value, $fieldName);
    }

	public static function getMemoryList($limit , $sqlSearch, $orderBy) {
		if (empty($sqlSearch)) {
			$sqlSearch = '1=1';
		}
		if (empty($orderBy)) {
			$orderBy = ' ORDER BY `ml`.`id` ASC ';
		}
		$sql  = "SELECT `ml`.*, `pr`.`pr_name`, `ml`.`id` AS `ml_id`, `pg`.`pg_name`, `pg`.`id` AS `pg_id`, IF(`ml`.`id` = `mlp`.`mlp_memory_id`, 1, 0) AS `mlp_exist` FROM ?f AS `ml`
				LEFT JOIN ?f AS `pg` ON `pg`.`id` = `ml`.`ml_gallery_id` AND `pg`.`deleted` = 0
				LEFT JOIN ?f AS `mlp` ON `ml`.`id` = `mlp`.`mlp_memory_id` AND `mlp`.`deleted` = 0
				LEFT JOIN ?f AS `pr` ON `pr`.`id` = `ml`.`ml_project_id` AND `pr`.`deleted` = 0
				WHERE `ml`.`deleted` = 0 AND {$sqlSearch}
				GROUP BY `ml`.`id` {$orderBy} {$limit} ;";

		return self::$DB->select($sql, self::$tableName, TAB_PHOTO_GALLERIES, TAB_MEMORY_LIST_PRODUCTS, TAB_PROJECTS);
	}

	public static function getTotalMemoryList($sqlCondition = '1=1') {
		if (empty($sqlCondition)) {
			$sqlCondition = '1=1';
		}
		$sql  = "SELECT COUNT(DISTINCT `ml`.`id`) FROM ?f AS `ml`
				LEFT JOIN ?f AS `pg` ON `pg`.`id` = `ml`.`ml_gallery_id` AND `pg`.`deleted` = 0
				LEFT JOIN ?f AS `mlp` ON `ml`.`id` = `mlp`.`mlp_memory_id` AND `mlp`.`deleted` = 0
				LEFT JOIN ?f AS `pr` ON `pr`.`id` = `ml`.`ml_project_id` AND `pr`.`deleted` = 0
				WHERE `ml`.`deleted` = 0 AND {$sqlCondition} ;";

		return self::$DB->selectCell($sql, self::$tableName, TAB_PHOTO_GALLERIES, TAB_MEMORY_LIST_PRODUCTS, TAB_PROJECTS);

	}

	/**
	 * @param $productId
	 * @param $memoryIdentifier
	 * @param $themeId
	 * @return bool
	 */
	public function addProductToMemoryList($productId, $memoryIdentifier, $themeId) {

		if (!$productId || !$memoryIdentifier) {
			return false;
		}

		$this->id = null;
		$this->ml_memory_identifier = $memoryIdentifier;
		$this->ml_project_id = self::$PROJECT['id'];
		$this->save(true);

		$memoryProductListObj = new Db_Products_MemoryListProducts();

		$getVersion = $memoryProductListObj->findByFields(array('mlp_memory_id' => $this->id,'mlp_product_id' => $productId), 'AND', 'id DESC');
		$version = ($getVersion ? (int)$getVersion->mlp_version + 1 : null );
		$timeNow = time();
		$mlpName = 'Version '.$version .' ' . date('H:i',$timeNow). ' | ' . Utils::formatDateUser($timeNow,false);



		$memoryProductListObj->id = null;
		$memoryProductListObj->mlp_memory_id = $this->id;
		$memoryProductListObj->mlp_product_id = $productId;
		$memoryProductListObj->mlp_theme_id = $themeId;
		$memoryProductListObj->mlp_version = $version;
		$memoryProductListObj->mlp_name = $mlpName;

		if ($memoryProductListObj->save() ) {
			$dataReturn = array(
				'id' => Utils::memoryProductIdEncode($memoryProductListObj->id),
				'mlp_name' => $mlpName
			);
			return $dataReturn;
		} else {
			return false;
		}

	}

	/**
	 * @static
	 * @param $memoryIdentifier
	 * @return bool|mixed
	 */
	public static function getMemoryProductsByIdentifier($memoryIdentifier) {
		if (empty($memoryIdentifier)) {
			return false;
		}
		$sql = "SELECT ml.id FROM ?f ml
				INNER JOIN ?f mlp ON ml.id = mlp.mlp_memory_id AND mlp.deleted = 0
				WHERE ml.deleted = 0 AND ml.ml_memory_identifier = ?
		";
		return self::$DB->selectCell($sql, self::$tableName, TAB_MEMORY_LIST_PRODUCTS, $memoryIdentifier);
	}


}
